/**
 *
 */
package com.whosly.rapid.lang.thread.executor;

import java.lang.reflect.Method;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import com.whosly.rapid.lang.thread.factory.NamedThreadFactory;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yueny09 <yueny09@163.com>
 *
 * @DATE 2017年11月12日 下午1:03:17
 *
 */
@Slf4j
@Deprecated
public class TraceAwareThreadPoolExecutor extends AbstractTraceAwareThreadPool {
	/**
	 * Creates a new instance.
	 *
	 * @param corePoolSize
	 *            the maximum number of active threads
	 */
	public TraceAwareThreadPoolExecutor(final int corePoolSize) {
		this(corePoolSize, 30, TimeUnit.SECONDS);
	}

	/**
	 * Creates a new instance.
	 *
	 * @param corePoolSize
	 *            the maximum number of active threads
	 * @param keepAliveTime
	 *            the amount of time for an inactive thread to shut itself down
	 * @param unit
	 *            the {@link TimeUnit} of {@code keepAliveTime}
	 */
	public TraceAwareThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize, final long keepAliveTime,
			final TimeUnit unit) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, new NamedThreadFactory());
	}

	/**
	 * Creates a new instance.
	 *
	 * @param corePoolSize
	 *            the maximum number of active threads
	 * @param keepAliveTime
	 *            the amount of time for an inactive thread to shut itself down
	 * @param unit
	 *            the {@link TimeUnit} of {@code keepAliveTime}
	 * @param threadFactory
	 *            the {@link ThreadFactory} of this pool
	 */
	public TraceAwareThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize, final long keepAliveTime,
			final TimeUnit unit, final BlockingQueue<Runnable> workQueue, final ThreadFactory threadFactory) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);

		// Call allowCoreThreadTimeOut(true) using reflection
		// because it is not supported in Java 5.
		try {
			final Method m = getClass().getMethod("allowCoreThreadTimeOut", new Class[] { boolean.class });
			m.invoke(this, Boolean.TRUE);
		} catch (final Throwable t) {
			// Java 5
			log.debug("ThreadPoolExecutor.allowCoreThreadTimeOut() is not " + "supported in this platform.");
		}
	}

	/**
	 * Creates a new instance.
	 *
	 * @param corePoolSize
	 *            the maximum number of active threads
	 * @param keepAliveTime
	 *            the amount of time for an inactive thread to shut itself down
	 * @param unit
	 *            the {@link TimeUnit} of {@code keepAliveTime}
	 * @param threadFactory
	 *            the {@link ThreadFactory} of this pool
	 */
	public TraceAwareThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize, final long keepAliveTime,
			final TimeUnit unit, final ThreadFactory threadFactory) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<Runnable>(), threadFactory);
	}

	/**
	 * Creates a new instance.
	 *
	 * @param corePoolSize
	 *            the maximum number of active threads
	 * @param keepAliveTime
	 *            the amount of time for an inactive thread to shut itself down
	 * @param unit
	 *            the {@link TimeUnit} of {@code keepAliveTime}
	 */
	public TraceAwareThreadPoolExecutor(final int corePoolSize, final long keepAliveTime, final TimeUnit unit) {
		this(corePoolSize, corePoolSize, keepAliveTime, unit);
	}

	/**
	 * Creates a new instance.
	 *
	 * @param corePoolSize
	 *            the maximum number of active threads
	 * @param keepAliveTime
	 *            the amount of time for an inactive thread to shut itself down
	 * @param unit
	 *            the {@link TimeUnit} of {@code keepAliveTime}
	 * @param threadFactory
	 *            the {@link ThreadFactory} of this pool
	 */
	public TraceAwareThreadPoolExecutor(final int corePoolSize, final ThreadFactory threadFactory) {
		this(corePoolSize, corePoolSize, 0L, TimeUnit.MILLISECONDS, threadFactory);
	}

}
